#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
手机号码生成及处理系统启动脚本
"""

import sys
import os

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

def check_dependencies():
    """检查依赖包是否已安装"""
    required_packages = ['PyQt6', 'pandas', 'numpy']
    missing_packages = []
    
    for package in required_packages:
        try:
            __import__(package.lower().replace('-', '_'))
        except ImportError:
            missing_packages.append(package)
    
    return missing_packages

def install_dependencies(missing_packages):
    """安装缺失的依赖包"""
    import subprocess
    import sys
    
    for package in missing_packages:
        print(f"正在安装 {package}...")
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])
            print(f"{package} 安装成功")
        except subprocess.CalledProcessError as e:
            print(f"安装 {package} 失败: {e}")
            return False
    
    return True

def main():
    """主函数"""
    print("=" * 50)
    print("手机号码生成及处理系统")
    print("=" * 50)
    
    # 检查依赖
    missing_packages = check_dependencies()
    
    if missing_packages:
        print(f"发现缺失的依赖包: {', '.join(missing_packages)}")
        print("正在尝试自动安装...")
        
        if not install_dependencies(missing_packages):
            print("\n依赖包安装失败，请手动安装以下包:")
            for package in missing_packages:
                print(f"pip install {package}")
            input("按回车键退出...")
            return
        print("所有依赖包安装完成!")
    
    # 启动应用程序
    try:
        from phone_generator import main as app_main
        print("\n启动应用程序...")
        app_main()
    except Exception as e:
        print(f"启动应用程序时出错: {e}")
        input("按回车键退出...")

if __name__ == "__main__":
    main()